package org.hqs.patient.controller;

import org.hqs.feign.common.JsonResult;
import org.hqs.patient.pojo.Patient;
import org.hqs.patient.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;

    @GetMapping("/{orderId}")
    public JsonResult< Patient> queryById(@PathVariable("orderId") Long orderId) {
        // 根据id查询订单并返回
        return new JsonResult(patientService.queryPatientById(orderId));
    }

    @GetMapping("/findAll")
    public JsonResult<List<Patient>> findAllPatients() {
        return new JsonResult<>(patientService.findAllPatients());
    }

    @PostMapping("/add")
    public JsonResult addPatient(@RequestBody Patient patient) {
        int result = patientService.insertPatient(patient);
        if (result > 0) {
            return new JsonResult("200", "添加患者成功");
        } else {
            return new JsonResult("400", "添加患者失败");
        }
    }

    @PutMapping("/update")
    public JsonResult updatePatient(@RequestBody Patient patient) {
        int result = patientService.updatePatient(patient);
        if (result > 0) {
            return new JsonResult("200","更新患者信息成功");
        } else {
            return new JsonResult("400","更新患者信息失败");
        }
    }

    @DeleteMapping("/delete")
    public JsonResult deletePatient(@RequestBody Patient patient) {
        Long id = Long.valueOf(patient.getId());
        if (id != null) {
            int result = patientService.deletePatientById(id);
            if (result > 0) {
                return new JsonResult("200", "删除患者成功");
            } else {
                return new JsonResult( "400","删除患者失败");
            }
        } else {
            return new JsonResult("400", "无效的患者 ID");
        }
    }
}